package com.kitty.game.item.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.kitty.common.cache.RedisService;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.drop.model.product.FightDropSet;
import com.kitty.game.fight.service.FightService;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.bag.message.RespFinishSortPack;
import com.kitty.game.bag.message.RespFlushPack;
import com.kitty.game.drop.service.DropService;
import com.kitty.game.enter.RespNotifyMisc;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.item.ItemDataPool;
import com.kitty.game.team.message.RespMsg;
import com.kitty.mina.message.MessagePusher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**藏宝箱*/
@Component
public class CangBaoXiangHandler {
    private int MIN_LEVEL = 30;

    private static final String LEVEL_NOT_ENOUGH = "等级未达到#R{0}#n级，不能打开{1}。";
    private static final String NO_KEY = "包裹中没有#R钥匙#n，无法使用。";
    private static final String USE_TIP = "包裹中选择钥匙使用即可打开宝箱。";
    private static final String NO_BOX = "包裹中没有#R{0}#n，无法处理。";
    private static final String USE_SUCCESS_NO_LIMIT = "你使用#R{0}#n打开了{1}。";
    private static final String USE_SUCCESS_LIMIT = "你使用#R{0}#n打开了{1}，今天还能打开#R{2}#n个{1}。";

    @Autowired
    EquipService equipService;
    @Autowired
    DropService dropService;

    public static void loadCommonSet() {
        String cmoonSer = SpringUtils.getBean(RedisService.class).get("cang_bao_xiang_set");
        Map<String, List<String>> box2Key = JSON.parseObject(cmoonSer, new TypeReference<Map<String, List<String>>>() {});
        Map<String, String> key2Box = new HashMap<>();
        box2Key.forEach((box, list) -> list.forEach(((key) -> key2Box.put(key, box))));

        ItemDataPool.box2Key = box2Key;
        ItemDataPool.key2Box = key2Box;
    }

    /**是否是藏宝箱*/
    public boolean isBox(String itemName) {
        return ItemDataPool.box2Key.containsKey(itemName);
    }

    /**是否是藏宝箱的钥匙*/
    public boolean isBoxKey(String itemName) {
        return ItemDataPool.key2Box.containsKey(itemName);
    }

    /**根据宝箱名获得可开的钥匙列表*/
    public List<String> getKeys(String boxName) {
        return ItemDataPool.box2Key.get(boxName);
    }

    /**根据钥匙名获得宝箱名*/
    public String getBox(String keyName) {
        return ItemDataPool.key2Box.get(keyName);
    }

    /**使用宝箱*/
    public void useBox(Role role, String itemName) {
        if (isBagContainKey(role, itemName) == false) {
            String msg = NO_KEY;
            MessagePusher.pushMessage(role, new RespMsg(msg));
            return ;
        }

        String msg = USE_TIP;
        MessagePusher.pushMessage(role, new RespMsg(msg));
    }

    /**包裹 里是否有钥匙道具*/
    private boolean isBagContainKey(Role role, String boxName) {
        List<String> keys = getKeys(boxName);
        if (keys == null) {return false;}
        for (String key : keys) {
            int count = SpringUtils.getEquipService().getCount(key, true, role);
            if (count > 0) {
                return true;
            }
        }

        return false;
    }

    /**使用宝箱钥匙*/
    public void useBoxKey(Role role, RoleEquip roleEquip) {
        String itemName = roleEquip.getName();
        String boxName = getBox(itemName);

        /**是否有宝箱*/
        if (isBagContainBox(role, itemName) == false) {
            String msg = MessageFormat.format(NO_BOX, boxName);
            MessagePusher.pushMessage(role, new RespMsg(msg));
            return;
        }

        /**等级是否满足*/
        if (role.getLevel() < MIN_LEVEL) {
            String msg = MessageFormat.format(LEVEL_NOT_ENOUGH, MIN_LEVEL, boxName);
            MessagePusher.pushMessage(role, new RespMsg(msg));
            return ;
        }
        ActivityType item_drop = null;
        if("钥匙串".equals(itemName)){
            item_drop = ActivityType.item_drop_yaoshichuan;
        }else if("银钥匙".equals(itemName)){
            item_drop = ActivityType.item_drop_yinyaoshi;
        }else if("金钥匙".equals(itemName)){
            item_drop = ActivityType.item_drop_jinyaoshi;
        }else if("水晶钥匙".equals(itemName)){
            item_drop = ActivityType.item_drop_shuijingyaoshi;
        }else if("吉祥如意".equals(itemName)){
            item_drop = ActivityType.item_drop_jixiang;
        }else if("金玉满堂".equals(itemName)){
            item_drop = ActivityType.item_drop_jinyu;
        }
        if ( SpringUtils.getActivityService().getRemainCount(role, item_drop) <= 0) {
            MessagePusher.pushMessage(role,new RespMsg(MessageFormat.format("{0}使用已达每日上限!!",itemName)));
            return;
        }
        List<FightDropSet> itemDropSet = dropService.getItemDropSet(itemName);
        SpringUtils.getBean(FightService.class).forDropSet(role,itemDropSet,itemName);
        int activityCount = role.getActivity().getFinishCounts().getInt(item_drop.getActivityId(), 0);
        int count = activityCount + 1;
        role.getActivity().getFinishCounts().put(item_drop.getActivityId(), count);

        /**删除宝箱*/
        equipService.delRoloEquip(boxName, 1, true, role);
        /**删除钥匙*/
        equipService.delEquipNum(role, roleEquip, 1);

        /**刷新背包*/
        RespFlushPack respFlushPack = new RespFlushPack();
        respFlushPack.setType("bag");
        MessagePusher.pushMessage(role, respFlushPack);

        RespFinishSortPack respFinishSortPack = new RespFinishSortPack();
        respFinishSortPack.setStart_range((byte) 0);
        MessagePusher.pushMessage(role, respFinishSortPack);

        /**成功提示信息*/
        String msg;
        int remainDropCount = dropService.getRemainDropCount(role, boxName);
        if (remainDropCount < 0) {
            msg = MessageFormat.format(USE_SUCCESS_NO_LIMIT, itemName, boxName);
        } else {
            msg = MessageFormat.format(USE_SUCCESS_LIMIT, itemName, boxName, remainDropCount);
        }
        MessagePusher.pushMessage(role, new RespNotifyMisc(msg));
    }

    /**是否包裹包含宝箱*/
    private boolean isBagContainBox(Role role, String keyName) {
        String box = getBox(keyName);
        if (box == null) {return false;}

        return SpringUtils.getEquipService().getCount(box, true, role) > 0;
    }
}
